package com.mobilebsc.app;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.mobilebsc.R;
import com.mobilebsc.datamanager.IDataReadyListener;
import com.mobilebsc.datamanager.KPIDataManager;
import com.mobilebsc.datamanager.PerspectivesDataManager;
import com.mobilebsc.datamanager.StrategicGoalsDataManager;
import com.mobilebsc.datamanager.WeakSignalsDataManager;
import com.mobilebsc.datatype.EDataType;
import com.mobilebsc.datatype.KPI;
import com.mobilebsc.datatype.Perspective;
import com.mobilebsc.datatype.StrategicGoal;
import com.mobilebsc.datatype.WeakSignal;

public class KPIActivity extends Activity implements IDataReadyListener
{
    private LinearLayout              llMain;
    private int                       indexCount  = 1;
    private DataManager               dataManager = new DataManager();
    private PerspectivesDataManager   pdm         = new PerspectivesDataManager(this);
    private StrategicGoalsDataManager sgdm        = new StrategicGoalsDataManager(this);
    private KPIDataManager            kdm         = new KPIDataManager(this);
    private WeakSignalsDataManager    wsdm        = new WeakSignalsDataManager(this);
    private int                       perspectiveId;
    private int                       sgId;
    private int                       kpiId;
    private int                       loadState;
    private String                    parameterList;
    private String[]                  parameters;
    private Intent                    intentStrategicGoal;
    public static String              extra_message = "";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kpi);
        
        // Get the message from the intent
        Intent intent = getIntent();
        parameterList = intent
                .getStringExtra(PerspectiveActivity.extra_message);
        
        parameters = parameterList.split(";");
        
        perspectiveId = Integer.valueOf(parameters[0]);
        sgId = Integer.valueOf(parameters[1]);
        kpiId = Integer.valueOf(parameters[2]);
        
        Log.i("Test", "parameters: " + parameterList);
        Log.i("Test", "perspectiveId: " + Integer.toString(perspectiveId));
        Log.i("Test", "sgId: " + Integer.toString(sgId));
        Log.i("Test", "kpiId: " + Integer.toString(kpiId));

        llMain = (LinearLayout) findViewById(R.id.LinearLayout1);

        loadState = 0;
        pdm.getById(perspectiveId);

        TextView tvBC = (TextView) findViewById(R.id.textViewBC);
        tvBC.setText("Home > BSC > Perspective > Strategic Goal > KPI >");
    }

    private void showKPIs(ArrayList<KPI> kpis)
    {
        for (KPI k : kpis)
        {
            LinearLayout llKPI = new LinearLayout(this);

            TextView tvKPIName = new TextView(this);
            tvKPIName.setBackgroundColor(Color.rgb(220, 220, 220));
            tvKPIName.setText(k.getName());
            tvKPIName.setHint(k.getDescription());

            LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 3.5f);
            llp.setMargins(0, 2, 0, 2);

            tvKPIName.setLayoutParams(llp);
            tvKPIName.setPadding(2, 2, 2, 2);
            tvKPIName.setWidth(100);

            TextView tvKPIWSInf = new TextView(this);
            tvKPIWSInf.setBackgroundColor(calcBGColor(k.getThreshold1()));
            tvKPIWSInf.setText(String.valueOf(k.getThreshold1()));
            tvKPIWSInf.setGravity(Gravity.RIGHT);
            tvKPIWSInf.setWidth(25);
            tvKPIWSInf.setPadding(2, 0, 2, 4);
            tvKPIWSInf
                    .setLayoutParams(new LinearLayout.LayoutParams(
                            LayoutParams.WRAP_CONTENT,
                            LayoutParams.WRAP_CONTENT, 1.0f));

            TextView tvKPIValue = new TextView(this);
            tvKPIValue.setBackgroundColor(calcBGColor(k.getThreshold2()));
            tvKPIValue.setText(String.valueOf(k.getThreshold2()));
            tvKPIValue.setGravity(Gravity.RIGHT);
            tvKPIValue.setWidth(25);
            tvKPIValue.setPadding(2, 0, 2, 4);
            tvKPIValue
                    .setLayoutParams(new LinearLayout.LayoutParams(
                            LayoutParams.WRAP_CONTENT,
                            LayoutParams.WRAP_CONTENT, 1.0f));

            llKPI.addView(tvKPIName);
            llKPI.addView(tvKPIWSInf);
            llKPI.addView(tvKPIValue);

            indexCount++;

            llMain.addView(llKPI, indexCount);
            
            showWeakSignals(k.getWeakSignals());
        }
    }
    
    public int calcBGColor(double Value)
    {
        if (Value >= 8)
        {
            return Color.rgb(220, 220, 220);
        } 
        else if (Value >= 4)
        {
            return Color.rgb(255, 215, 0);
        } 
        else
        {
            return Color.rgb(178, 34, 34);
        }
    }
    
    private void showWeakSignals(ArrayList<WeakSignal> weakSignals)
    {
        for (WeakSignal w : weakSignals)
        {
            TextView tvW = new TextView(this);

            tvW.setText(w.getName());
            tvW.setHint(w.getDescription());
            tvW.setTextAppearance(this, R.style.mBSCSGName);
            tvW.setBackgroundColor(Color.GRAY);
            tvW.setPadding(5, 5, 5, 5);

            indexCount++;

            llMain.addView(tvW, indexCount);
        }
    }
    
    public void onClick_btn_Back(View view)
    {
        intentStrategicGoal = new Intent(this, StrategicGoalActivity.class);
        intentStrategicGoal.putExtra(extra_message, Integer.toString(perspectiveId) + ";" + Integer.toString(sgId));
        startActivity(intentStrategicGoal);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.kpi, menu);
        return true;
    }

    @Override
    public void dataReady()
    {
        boolean display = false;
        
        if (loadState == 0)
        {
            List<Perspective> list = pdm.loadById(perspectiveId);

            dataManager.setPerspectives((ArrayList<Perspective>) list);
            
            loadState = 1;
            
            sgdm.getById(sgId);
        }
        else if (loadState == 1)
        {
            List<StrategicGoal> list = sgdm.loadById(sgId);

            dataManager.getPerspectives().get(0).setStrategicGoals(((ArrayList<StrategicGoal>) list));
            
            loadState = 2;
            
            kdm.getReferencedKPI(sgId, EDataType.StrategicGoal);           
        }
        else if (loadState == 2)
        {
            List<KPI> list = kdm.loadById(kpiId);

            dataManager.getPerspectives().get(0).getStrategicGoals().get(0).setKpis((ArrayList<KPI>) list);
            
            loadState = 3;
            
            //wsdm.getAll();
            wsdm.getReferencedWeakSignals(kpiId, EDataType.KPI);
        }
        
        else if (loadState == 3)
        {
            List<WeakSignal> list = wsdm.loadAll();

            dataManager.getPerspectives().get(0).getStrategicGoals().get(0).getKpis().get(0).setWeakSignals((ArrayList<WeakSignal>) list);
            
            display = true;
        }
        
        if (display) showKPIs(dataManager.getPerspectives().get(0).getStrategicGoals().get(0).getKpis());
    }
}